package com.ruoyi.my.service.impl.courseplan;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.my.domian.courseplan.CoursePlanTeacherVO;
import com.ruoyi.my.mapper.courseplan.CoursePlanTeacherMapper;
import com.ruoyi.my.service.courseplan.CoursePlanTeacherService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CoursePlanTeacherServiceImpl implements CoursePlanTeacherService {

    @Autowired
    private CoursePlanTeacherMapper coursePlanTeacherMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Override
    public List<CoursePlanTeacherVO> selectCoursePlanTeacherList(CoursePlanTeacherVO coursePlanTeacherVO) {
        return coursePlanTeacherMapper.selectCoursePlanTeacherList(coursePlanTeacherVO);
    }

    @Override
    public AjaxResult sysUserToCoursePlanTeacher() {
        String createBy = SecurityUtils.getUserId().toString();
        //获取全部的长老用户
        SysUser sysUser = new SysUser();
        sysUser.setUserType("00");
        List<SysUser> sysUserList = userMapper.selectUserList(sysUser);
        //获取全部已导入的教师信息
        CoursePlanTeacherVO coursePlanTeacherVO = new CoursePlanTeacherVO();
        coursePlanTeacherVO.setDesc("1");
        List<CoursePlanTeacherVO> coursePlanTeacherVOList = coursePlanTeacherMapper.selectCoursePlanTeacherList(coursePlanTeacherVO);
        if(coursePlanTeacherVOList.size() > 0) {//有值
            //将已有的转为Set
            Set<Long> bKeys = coursePlanTeacherVOList.stream()
                    .map(CoursePlanTeacherVO::getUserId)
                    .collect(Collectors.toSet());
            //获取未导入的教师信息
            sysUserList = sysUserList.stream()
                    .filter(p -> !bKeys.contains(p.getUserId()))
                    .collect(Collectors.toList());
        }
        if(sysUserList.size() > 0) {
            //循环导入
            for(SysUser user: sysUserList) {
                CoursePlanTeacherVO planTeacherVO = new CoursePlanTeacherVO();
                planTeacherVO.setUserId(user.getUserId());
                planTeacherVO.setTeacherCode(String.valueOf(user.getUserId() + 10000L));
                planTeacherVO.setTeacherName(user.getNickName());
                planTeacherVO.setCreateBy(createBy);
                coursePlanTeacherMapper.insertCoursePlanTeacher(planTeacherVO);
            }
            return AjaxResult.success("已导入" + sysUserList.size() + "个教师");
        }
        return AjaxResult.success("已经全部导入！");
    }

    @Override
    public int deleteCoursePlanTeacherByIds(Long[] userIds) {
        return coursePlanTeacherMapper.deleteCoursePlanTeacherByIds(userIds);
    }

    @Override
    public int updateCoursePlanTeacherById(CoursePlanTeacherVO coursePlanTeacherVO) {
        coursePlanTeacherVO.setUpdateBy(SecurityUtils.getUserId().toString());
        return coursePlanTeacherMapper.updateCoursePlanTeacherById(coursePlanTeacherVO);
    }
}
